<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>二元操作示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fc;
            margin: 0;
            padding: 0;
        }

        .container {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            flex-direction: column;
        }

        h1 {
            color: #3b3f58;
            margin-bottom: 20px;
        }

        .input-group {
            margin-bottom: 20px;
        }

        .input-group input {
            padding: 10px;
            font-size: 16px;
            border-radius: 5px;
            border: 1px solid #ccc;
            margin-right: 10px;
            width: 100px;
        }

        .btn {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
            border-radius: 5px;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            padding: 15px;
            background-color: #fff;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            width: 300px;
            text-align: center;
        }

        .result p {
            font-size: 18px;
            color: #3b3f58;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>二元操作示例：选择一个操作并输入两个数字</h1>
        <div class="input-group">
            <input type="number" id="inputA" placeholder="数字 A">
            <input type="number" id="inputB" placeholder="数字 B">
            <button class="btn" onclick="performOperation()">执行操作</button>
        </div>

        <div class="result" id="result">
            <p>结果将在这里显示</p>
        </div>
    </div>

    <script>
        // 定义binary函数，接受一个handler函数
        const binary = handler => (a, b) => handler(a, b);

        // 定义操作处理函数，例如加法
        function add(a, b) {
            return a + b;
        }

        function subtract(a, b) {
            return a - b;
        }

        function multiply(a, b) {
            return a * b;
        }

        function divide(a, b) {
            return b !== 0 ? a / b : "除数不能为零";
        }

        // 执行操作的函数
        function performOperation() {
            // 获取输入的值
            const a = parseFloat(document.getElementById('inputA').value);
            const b = parseFloat(document.getElementById('inputB').value);

            // 判断输入是否合法
            if (isNaN(a) || isNaN(b)) {
                document.getElementById('result').innerHTML = '<p style="color: red;">请输入有效的数字！</p>';
                return;
            }

            // 使用binary函数执行加法、减法、乘法、除法
            const addOperation = binary(add);
            const subtractOperation = binary(subtract);
            const multiplyOperation = binary(multiply);
            const divideOperation = binary(divide);

            // 获取各个操作结果
            const sum = addOperation(a, b);
            const difference = subtractOperation(a, b);
            const product = multiplyOperation(a, b);
            const quotient = divideOperation(a, b);

            // 显示结果
            document.getElementById('result').innerHTML = `
                <p>加法结果: ${sum}</p>
                <p>减法结果: ${difference}</p>
                <p>乘法结果: ${product}</p>
                <p>除法结果: ${quotient}</p>
            `;
        }
    </script>
</body>

</html>